//
//  SortInforViewController.m
//  XiMaLaYa
//
//  Created by dllo on 15/11/14.
//  Copyright © 2015年 L-G. All rights reserved.
//

#import "LGCSortInforViewController.h"
#import "DetailCollectionViewCell.h"
#import "LGCInforViewController.h"
#import "GetConnection.h"
#import "Whatever.h"


#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375

@interface LGCSortInforViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,WhateverDelegate>

@property (nonatomic, retain)UICollectionView *collectionView;

@property (nonatomic, retain)NSMutableArray *arrayData;

@property (nonatomic, retain)NSMutableArray *arrayTitle;

@property (nonatomic, retain)Whatever *whatever;

@end

@implementation LGCSortInforViewController
- (void)dealloc
{
    [_arrayData release];
    [_arrayTitle release];
    [_collectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = self.strTitle;
    
    self.arrayTitle = [NSMutableArray array];
    self.arrayData = [NSMutableArray array];
    
    [self connection];
    [self createSubviews];
    self.automaticallyAdjustsScrollViewInsets = NO;
}
#pragma mark 创建视图
- (void)createSubviews
{
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    btnBack.frame = CGRectMake(0, 0, 30 * WIDTH, 30 * HEIGHT);
    [btnBack setImage:[UIImage imageNamed:@"back@2x"] forState:UIControlStateNormal];
    [btnBack addTarget:self action:@selector(btnBackAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,50 * HEIGHT, self.view.frame.size.width, self.view.frame.size.height - 50 * HEIGHT) collectionViewLayout:flowLayout];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.collectionView.pagingEnabled = YES;
    
    self.collectionView.delegate = self;
    
    self.collectionView.dataSource = self;
    
    [self.view addSubview:self.collectionView];
    
    [_collectionView release];
    // 注册
    [self.collectionView registerClass:[DetailCollectionViewCell class] forCellWithReuseIdentifier:@"detailCell"];
}
// 索引btn的点击事件
- (void)buttonOfWhateverClicked:(UIButton *)sender
{
    self.collectionView.contentOffset = CGPointMake((sender.tag - 1) * self.view.frame.size.width,0);
}
// collectionView的滑动事件，与索引相关联
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.btnTag = self.collectionView.contentOffset.x / self.view.frame.size.width + 1;
    self.whatever.btntag = self.btnTag;
}
- (void)btnBackAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
#pragma mark 返回每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 100 * HEIGHT);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrayTitle.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"detailCell" forIndexPath:indexPath];
    cell.strID = self.strID;
    cell.strTitle = self.arrayTitle[indexPath.row];
    cell.customBlock = ^(NSString *albumid){
        [self sendAnAlbumid:albumid];
    };
    return cell;
}
- (void)sendAnAlbumid:(NSString *)albumid
{
    LGCInforViewController *Infor = [LGCInforViewController shareLGCInformation];
    Infor.strID = albumid;
    [self.navigationController pushViewController:Infor animated:YES];
}
- (void)connection
{
    NSString *str= [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/tagsWithoutCover?categoryId=%@&contentType=album&device=iPhone",self.strID];
    [GetConnection startGETConnection:str parmaters:nil connectionBlock:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *arrayList = [dic objectForKey:@"list"];
        for (NSDictionary *temp in arrayList) {
            NSString *str = [temp objectForKey:@"tname"];
            [self.arrayTitle addObject:str];
        }
        [self.collectionView reloadData];
        
        self.whatever = [[Whatever alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 50 * HEIGHT) BtnTitleArray:self.arrayTitle];
        self.whatever.delegate = self;
        [self.view addSubview:self.whatever];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
